package com.iknow.xmpp.service;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;
import com.iknow.util.Loger;
import com.iknow.xmpp.smack.avatar.AvatarCache;
import com.iknow.xmpp.smack.avatar.AvatarManager;
import com.iknow.xmpp.smack.avatar.AvatarMetadataExtension;
import com.iknow.xmpp.smack.avatar.AvatarMetadataExtension.Info;
import com.iknow.xmpp.smack.pep.PepSubManager;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import org.jivesoftware.smack.Connection;

public class IKnowAvatarManager extends AvatarManager
{
  private static final int JPEG_QUALITY = 100;
  private static final String TAG = IKnowAvatarManager.class.getSimpleName();
  private Context mContext;

  public IKnowAvatarManager(Context paramContext, Connection paramConnection, PepSubManager paramPepSubManager, AvatarCache paramAvatarCache, boolean paramBoolean)
  {
    super(paramConnection, paramPepSubManager, paramAvatarCache, paramBoolean);
    this.mContext = paramContext;
  }

  private byte[] getBitmapByte(Bitmap paramBitmap, Bitmap.CompressFormat paramCompressFormat, int paramInt)
  {
    byte[] arrayOfByte;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    if (paramBitmap.compress(paramCompressFormat, paramInt, localByteArrayOutputStream))
      arrayOfByte = localByteArrayOutputStream.toByteArray();
    while (true)
    {
      return arrayOfByte;
      arrayOfByte = new byte[0];
    }
  }

  private boolean publishAvatar(Bitmap paramBitmap)
  {
    int i;
    AvatarMetadataExtension localAvatarMetadataExtension = new AvatarMetadataExtension();
    AvatarMetadataExtension.Info localInfo1 = publishBitmap(paramBitmap, Bitmap.CompressFormat.JPEG, 100);
    AvatarMetadataExtension.Info localInfo2 = publishBitmap(paramBitmap, Bitmap.CompressFormat.PNG, 100);
    if (localInfo2 == null)
      i = 0;
    while (true)
    {
      return i;
      localAvatarMetadataExtension.addInfo(localInfo2);
      if (localInfo1 != null)
        localAvatarMetadataExtension.addInfo(localInfo1);
      publishAvatarMetaData(localInfo2.getId(), localAvatarMetadataExtension);
      i = 1;
    }
  }

  private AvatarMetadataExtension.Info publishBitmap(Bitmap paramBitmap, Bitmap.CompressFormat paramCompressFormat, int paramInt)
  {
    Object localObject;
    byte[] arrayOfByte;
    try
    {
      arrayOfByte = getBitmapByte(paramBitmap, paramCompressFormat, paramInt);
      String str1 = getAvatarId(arrayOfByte);
      if (!(publishAvatarData(arrayOfByte)))
      {
        localObject = null;
      }
      else
      {
        String str2 = "image/png";
        if (Bitmap.CompressFormat.JPEG == paramCompressFormat)
          str2 = "image/jpeg";
        AvatarMetadataExtension.Info localInfo = new AvatarMetadataExtension.Info(str1, str2, arrayOfByte.length);
        localInfo.setHeight(paramBitmap.getHeight());
        localInfo.setWidth(paramBitmap.getWidth());
        localObject = localInfo;
      }
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      localObject = null;
    }
    return localObject;
  }

  public boolean publishAvatar(Uri paramUri)
  {
    boolean bool1;
    boolean bool2;
    try
    {
      bool2 = publishAvatar(MediaStore.Images.Media.getBitmap(this.mContext.getContentResolver(), paramUri));
      bool1 = bool2;
      return bool1;
    }
    catch (IOException localIOException)
    {
      Loger.d(TAG, "Error while publishing avatar " + paramUri, localIOException);
      bool1 = false;
    }
  }
}